<script setup>
import GoodsItem from '@/components/GoodsItem.vue'
import { addGoodsApi, searchGoodsApi, updateGoodsApi } from '@/api/goods.js'
import { onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useUserStore } from '@/stores/userStore.js'
import GoodsDrawer from '@/components/GoodsDrawer.vue'

const route = useRoute()
const goodsList = ref([])
const userStore = useUserStore()
const getGoodsList = async () => {
  const { data } = await searchGoodsApi(route.query)
  goodsList.value = data
}
onMounted(() => getGoodsList())
// 监听路由变化
watch(route, () => getGoodsList())
const handleAddGoods=()=>{

}
const goodsDrawer = ref()
const openDrawer = () => {
  goodsDrawer.value.drawer = true
}
// 提交修改
const handleSubmit = async (goods) => {
  await addGoodsApi(goods)
  ElMessage.success('添加成功')
  goodsDrawer.value.drawer = false
}
</script>

<template>
  <div class="goods">
    <div class="title">
      <div class="left">
        <div v-for="item in 3" :key="item" style="width: 4px;height: 100%;background-color:#C20002;"></div>
      </div>
      <div class="center">老年用品</div>
      <div class="right">
        <div v-for="(item,index) in 3" :key="item"
             :style="{transform:'translateX(-'+16*(index+1)+'px)'}"
             style="clip-path: polygon(0 0, 20% 0%, 100% 70%, 50% 100%,30% 100%, 80% 70%,0 0);width: 28px;height: 100%;background:#C20002;"></div>
      </div>
      <el-button color="#39c5bb" icon="Plus" plain style="position: absolute;right: 5%" v-if="userStore.user?.role===0" @click="openDrawer ">添加商品</el-button>
    </div>
    <div class="content">
      <div v-for="item in goodsList" :key="item" class="content-item">
        <GoodsItem :goods="item"></GoodsItem>
      </div>
    </div>
  </div>
  <GoodsDrawer ref="goodsDrawer" :title="'添加商品'"
               @onSubmit="handleSubmit"></GoodsDrawer>
</template>

<style lang="scss" scoped>
.goods {
  padding: 20px;

  .title {
    display: flex;
    height: 35px;
    align-items: center;
    border-bottom: 3px solid #f1f1f1;
    padding-bottom: 5px;
    position: relative;

    .center {
      width: 150px;
      display: flex;
      align-items: center;
      font-size: 16px;
      color: #ffffff;
      padding: 0 10px;
      height: 100%;
      background-color: $lt-title-second-color;
      clip-path: polygon(0 0, 85% 0%, 100% 70%, 90% 100%, 0 100%);
    }

    .left {
      height: 100%;
      display: flex;
      gap: 2px;
      margin-right: 2px;
    }

    .right {
      height: 100%;
      display: flex;
      gap: 2px;
      margin-right: 2px;
    }
  }

  .content {
    margin-top: 30px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    row-gap: 30px;
    column-gap: 20px;

    &-item {
      transition: all 0.2s ease-in-out;
      display: flex;
      justify-content: center;
      //&:hover{
      //  transform: scale3d(1.05, 1.05, 1.05);
      //  box-shadow: 0 0 10px rgba(0,0,0,0.5);
      //}
    }
  }
}
</style>